<div class="content_wrapper">
  <div class="content_wrapper_inner">
    <script>
     $(document).ready(function(){
       $("#booknow").click(function(event){

    	   var $dep = $("input[name='sel_depart']:checked").val();
		   var $arr = $("input[name='sel_return']:checked").val();
			
			if ($dep == null) {
			 alert("<?php echo $this->lang->line('select_depart');?>");
	    	 event.preventDefault();
	         return;
			} 
	       	if ($("input[name='trip_type']:checked").val() == "RT" && $arr == null) {
				 alert("<?php echo $this->lang->line('select_return');?>");
		    	 event.preventDefault();
		         return;
	       	}
				
    	   	$dep = $("input[name='sel_depart']:checked").val().split('|');
			$arr = $("input[name='sel_return']:checked").val().split('|');

			var $dep_time=$dep[3];
         	if ($dep[0] == 2) {
          		$dep_time=$dep[9];
         	}
		 	if ($dep_time[2] == '-') {
		  		$dep_time=$dep_time.substr(3);
		 	}
			$dep_time = $dep_time.substr(0,2)+":"+$dep_time.substr(2)+":00";
			var str1 = $("input[name='depart_on']").val();
		    var dt1  = str1.substring(0,2);
		    var mon1 = str1.substring(3,5);
		    var yr1  = str1.substring(6,10);
		    temp1 = mon1 +"/"+ dt1 +"/"+ yr1 + " "+ $dep_time;
		    var $dep_flight = Date.parse(temp1);

			var $now = Date.now();	
			$diff = $dep_flight - $now;
		    if ($diff < 10900000) {
			 	alert("<?php echo $this->lang->line('flight_closed');?>");
			 	event.preventDefault();
		     	return;
			}
	    
		    var $arr_time=$arr[3];
			if ($arr_time[2] == '-') {
			  $arr_time=$arr_time.substr(3);
			}
			$arr_time = $arr_time.substr(0,2)+":"+$arr_time.substr(2)+":00";
		        
		    var str2 = $("input[name='return_on']").val();
		    var dt2  = str2.substring(0,2);
		    var mon2 = str2.substring(3,5);
		    var yr2  = str2.substring(6,10);
		    temp2 = mon2 +"/"+ dt2 +"/"+ yr2 + " "+ $arr_time;
		    var $arr_flight = Date.parse(temp2);
	
		    $diff = $arr_flight - $dep_flight;
		
		    if ($diff > 10800000 ) {
		         $("#bookflight").submit();
		    }
		    else {
				alert("<?php echo $this->lang->line('date_prior');?>");
		         event.preventDefault();
		    }
		});
    });

    $(document).ready(function(){
       $("#next_day_depart").click(function(event){
         $("input[name='depart_on']").val("<?php echo date("d.m.Y",strtotime(date("d-m-Y", strtotime($this->input->post('depart_on'))) . " +1 day"));?>");
         $("#searchflight").submit();
         event.preventDefault();
       });
     });
    $(document).ready(function(){
       $("#pre_day_depart").click(function(event){
         $("input[name='depart_on']").val("<?php echo date("d.m.Y",strtotime(date("d-m-Y", strtotime($this->input->post('depart_on'))) . " -1 day"));?>");
         $("#searchflight").submit();
         event.preventDefault();
       });
     });
    $(document).ready(function(){
        $("#next_day_return").click(function(event){
          $("input[name='return_on']").val("<?php echo date("d.m.Y",strtotime(date("d-m-Y", strtotime($this->input->post('return_on'))) . " +1 day"));?>");
          $("#searchflight").submit();
          event.preventDefault();
        });
      });
      $(document).ready(function(){
        $("#pre_day_return").click(function(event){
          $("input[name='return_on']").val("<?php echo date("d.m.Y",strtotime(date("d-m-Y", strtotime($this->input->post('return_on'))) . " -1 day"));?>");
          $("#searchflight").submit();
          event.preventDefault();
        });
    });
    </script>
	<script>
  $(document).ready(function() {
    $("button").button();
  });
  </script>
  	<?php $enable_https='http://'; /* to enable put s after http */
  	echo str_replace('http://',$enable_https , form_open('bookflight',array('id'=>'bookflight','method'=>'get'))); ?>
    <div class="depart">
	<div align="center">
	  <h4><?php echo $this->lang->line('select_depart');?></h4>
	  </div>
	  <div style="text-align:center;"><button id="pre_day_depart"><?php echo $this->lang->line('previous_day');?></button><span style="padding: 0 8px 0 8px;"><?php echo $this->input->post('depart_on'); ?></span><button id="next_day_depart"><?php echo $this->lang->line('next_day');?></button></div>
	  <?php
	    if (count($flight_depart) == 0){
        	echo '<div class="content" >'. $this->lang->line('no_available_flight_for_today') .'</div>';
      	} else {
      		?><br>
      		<table class="tb_available"> 
			<thead>
				<tr>
					<th>Uçuş</th>
					<th>Pro</th>
					<th>Eko</th>
					<th>Bus</th>	
				</tr>
			</thead>
			<tbody>
      		<?php  
		    foreach($flight_depart as $flight_details)
			{
			   	$flight = $flight_details['flight'];
				$leg = '<img src="/images/logo_'.$flight['leg1']["operatingairline"].'.png" alt="">'. $flight['leg1']["origin"] .'-'. $flight['leg1']["destination"] .' '. $flight['leg1']['flightno'] .' '. $flight['leg1']['crafttype'] .' '. $this->commonlibrary->format_flighttime($flight['leg1']["departureTime"]) .' '. $this->commonlibrary->format_flighttime($flight['leg1']["arrivalTime"]) .' '. $flight['leg1']['nextDay'];
			   	if (isset($flight['leg2'])) { 
			   		$leg .= '<br/><img src="/images/logo_'.$flight['leg2']['operatingairline'].'.png" alt="">'. $flight['leg2']['origin'] .'-'. $flight['leg2']["destination"] .' '. $flight['leg2']['flightno'] .' '. $flight['leg2']['crafttype'] .' '.  $this->commonlibrary->format_flighttime($flight['leg2']["departureTime"]) .' '. $this->commonlibrary->format_flighttime($flight['leg2']["arrivalTime"]) .' '. $flight['leg2']['nextDay'];
			   	}
			   	?> 
	               <tr>
						<td> <?php echo $leg;?> </td>
						<td style="text-align:center;"> <?php echo ($flight_details['total_amount_ecr'] != 0) ? '<input type="radio" value="'.$flight_details['key_ecr'].'" name="sel_depart"><br/>'.number_format($flight_details['total_amount_ecr']) : '-X-' ;?></td>
						<td style="text-align:center;"> <?php echo ($flight_details['total_amount_eco'] != 0) ? '<input type="radio" value="'.$flight_details['key_eco'].'" name="sel_depart"><br/>'.number_format($flight_details['total_amount_eco']) : '-X-' ;?></td>	
						<td style="text-align:center;"> <?php echo ($flight_details['total_amount_bus'] != 0) ? '<input type="radio" value="'.$flight_details['key_bus'].'" name="sel_depart"><br/>'.number_format($flight_details['total_amount_bus']) : '-X-' ;?></td>		
					</tr>
				<?php           
	        }
	        ?>
	        </tbody>
	        </table>
	        <?php
      	}
	 ?>
	</div>
    <?php if(strcmp($this->input->post('trip_type'),'RT')==0){?> <!-- TODO: check whether is return availability -->
      <div class="return">
	  <div align="center">
	   <h4><?php echo $this->lang->line('select_return');?></h4>
	  </div>
      <div style="text-align:center;"><button id="pre_day_return"><?php echo $this->lang->line('previous_day');?></button><span style="padding: 0 8px 0 8px;"><?php echo $this->input->post('return_on'); ?></span><button id="next_day_return"><?php echo $this->lang->line('next_day');?></button></div>
      <?php
	    if (count($flight_return) == 0){
        	echo '<div class="content" >'. $this->lang->line('no_available_flight_for_today') .'</div>';
      	} else {
      		?><br>
      		<table class="tb_available"> 
			<thead>
				<tr>
					<th>Uçuş</th>
					<th>Pro</th>
					<th>Eko</th>
					<th>Bus</th>	
				</tr>
			</thead>
			<tbody>
      		<?php  
		    foreach($flight_return as $flight_details)
			{
			   	$flight = $flight_details['flight'];
				$leg = '<img src="/images/logo_'.$flight['leg1']["operatingairline"].'.png" alt="">'. $flight['leg1']["origin"] .'-'. $flight['leg1']["destination"] .' '. $flight['leg1']['flightno'] .' '. $flight['leg1']['crafttype'] .' '. $this->commonlibrary->format_flighttime($flight['leg1']["departureTime"]) .' '. $this->commonlibrary->format_flighttime($flight['leg1']["arrivalTime"])  .' '. $flight['leg1']['nextDay'];
			   	if (isset($flight['leg2'])) { 
			   		$leg .= '<br/><img src="/images/logo_'.$flight['leg2']['operatingairline'].'.png" alt="">'. $flight['leg2']['origin'] .'-'. $flight['leg2']["destination"] .' '. $flight['leg2']['flightno'] .' '. $flight['leg2']['crafttype'] .' '.  $this->commonlibrary->format_flighttime($flight['leg2']["departureTime"]) .' '. $this->commonlibrary->format_flighttime($flight['leg2']["arrivalTime"]) .' '. $flight['leg2']['nextDay'];
			   	}
			   	?> 
	               <tr>
						<td> <?php echo $leg;?> </td>
						<td style="text-align:center;"> <?php echo ($flight_details['total_amount_ecr'] != 0) ? '<input type="radio" value="'.$flight_details['key_ecr'].'" name="sel_return"><br/>'.number_format($flight_details['total_amount_ecr']) : '-X-' ;?></td>
						<td style="text-align:center;"> <?php echo ($flight_details['total_amount_eco'] != 0) ? '<input type="radio" value="'.$flight_details['key_eco'].'" name="sel_return"><br/>'.number_format($flight_details['total_amount_eco']) : '-X-' ;?></td>	
						<td style="text-align:center;"> <?php echo ($flight_details['total_amount_bus'] != 0) ? '<input type="radio" value="'.$flight_details['key_bus'].'" name="sel_return"><br/>'.number_format($flight_details['total_amount_bus']) : '-X-' ;?></td>		
					</tr>
				<?php           
	        }
	        ?>
	        </tbody>
	        </table>
	        <?php
      	}
	 ?>
	  </div>
    <?php }?>
  <div align="right" style="width:100%; float:right; padding-right:20px;"><br><input id="booknow" type="submit" value="<?php echo $this->lang->line('continue');?>"></div>
  <?php echo form_close();?>
    </div>
  </div> 
	
